package com.lovo.Stream;


import java.io.*;

public class HomeWork {
    //1.拷贝
    public void copy() {
        BufferedOutputStream output = null;//输出
        BufferedInputStream input = null;//输入
        try {
            output = new BufferedOutputStream(new FileOutputStream("c:/learn/aa.txt"));
            input = new BufferedInputStream(new FileInputStream("c:/learn/s.txt"));
            //新建一个数组，作为中转
            byte[] bs = new byte[1024];
            int a = 0;
            //先输入给数组
            while ((a = input.read(bs)) != -1) {
                output.write(bs, 0, a);//(放在哪，从哪开始，长度是多少)
                output.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    output.close();
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    //2.追加
    public void supplement() {
        FileWriter fw =null;
        try {
            fw = new  FileWriter("c:/learn/a.txt",true);
            fw.append("加入一串中文语句");
            fw.flush();
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if (fw != null){
                try {
                    fw.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        HomeWork hm = new HomeWork();
        //调用copy
        hm.copy();
        //调用supplement
        hm.supplement();
    }
}
